//!
/**
 ***********************************************************************************************************
 * <RBRenderPassFullScreen Implementation>
 ***********************************************************************************************************
 * @file RBRenderPassFullScreen.cpp
 ***********************************************************************************************************/

/***********************************************************************************************************
 * Precompiled Header
 ***********************************************************************************************************/
#include "RBMainPCH.h"

/***********************************************************************************************************
 * Includes
 ***********************************************************************************************************/
#include "RBRenderPassFullScreen.h"

#include "RBHardwareBufferManager.h"
#include "RBGeometry.h"
#include "RBMaterial.h"
#include "RBRenderProcess/RBEffectFactory.h"
#include "RBRenderProcess/RBEffect.h"
#include "RBRenderProcess/RBEffectVariable.h"
#include "RBRenderProcess/RBEffectTechnique.h"
#include "RBMain/RBRenderPass.h"
#include "RBVector.h"

/***********************************************************************************************************
 * Defines
 ***********************************************************************************************************/

/***********************************************************************************************************
 * Classes implementation
 ***********************************************************************************************************/
uint32 RBRenderPassFullScreen::ms_uiFullScreenPasses = 0;
RBHandle<RBGeometry> RBRenderPassFullScreen::ms_pFullScreenTriangle = InvalidHandle;

/***********************************************************************************************************
 *
 ***********************************************************************************************************/
RBRenderPassFullScreen::RBRenderPassFullScreen(const RBString& strTechniqueName)
{
    m_pEffect = RBEffectFactory::GetSingleton()->RetrieveEffect(strTechniqueName);
    m_pMaterial = new RBMaterial();
    m_pMaterial->IncRefCount();
    m_pMaterial->SetEffect(m_pEffect);
    m_pMaterial->DepthState.ZWriteEnable = false;
    if (ms_uiFullScreenPasses++ == 0)
    {
        float3 pPositions[3]; pPositions[0] = float3(-1,3,1); pPositions[1] = float3(-1,-1,1); pPositions[2] = float3(3,-1,1);
        float2 pTextures[3] ; pTextures [0] = float2(0,-1)  ; pTextures [1] = float2(0,1)    ; pTextures [2] = float2(2,1)    ;
        uint16 pIndices[3]  ; pIndices  [0] = 0             ; pIndices  [1]   = 1            ; pIndices  [2]   = 2            ;

		ms_pFullScreenTriangle = RBGeometry::CreateGeometry(3, 3, pPositions, pIndices, RBGeometryPrimitiveType::TRIANGLELIST, m_pEffect->GetTechnique(0)->GetPass(0), NULL, pTextures, 1);
    }
}

/***********************************************************************************************************
 *
 ***********************************************************************************************************/
RBRenderPassFullScreen::~RBRenderPassFullScreen()
{
    m_pMaterial->DecRefCount();
    if (--ms_uiFullScreenPasses == 0)
    {
        RBGeometry::Destroy(ms_pFullScreenTriangle);
    }
}

/***********************************************************************************************************
 *
 ***********************************************************************************************************/
RBMaterial* RBRenderPassFullScreen::GetMaterial() const
{
    return m_pMaterial;
}

/***********************************************************************************************************
 *
 ***********************************************************************************************************/
RBEffect* RBRenderPassFullScreen::GetEffect() const
{
    return m_pEffect;
}

/***********************************************************************************************************
 *
 ***********************************************************************************************************/
void RBRenderPassFullScreen::SetEffect(RBEffect* pTechnique)
{
    m_pEffect = pTechnique;
}

/***********************************************************************************************************
 *
 ***********************************************************************************************************/
void RBRenderPassFullScreen::Render(RBRenderer* pRenderer, RBRenderTargetGroup* pRTT)
{
    ms_pFullScreenTriangle->SetMaterial(m_pMaterial);

    rb_render_pass::open_render_target_group(pRenderer, pRTT, 0);
    pRenderer->Render(ms_pFullScreenTriangle);
}
